package com.ruoyi.web.ydz.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.AttachFile;
import com.ruoyi.web.ydz.domain.Invitation;
import com.ruoyi.web.ydz.domain.InvitationCollect;
import com.ruoyi.web.ydz.mapper.InvitationCollectMapper;
import com.ruoyi.web.ydz.service.IAttachFileService;
import com.ruoyi.web.ydz.service.IInvitationCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 用户收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class InvitationCollectServiceImpl implements IInvitationCollectService {
    @Resource
    private InvitationCollectMapper invitationCollectMapper;

    @Resource
    private IAttachFileService AttachFileService;

    /**
     * 查询用户收藏
     *
     * @param id 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public InvitationCollect selectInvitationCollectById(Long id) {

        /*如果当前用户查询收藏详情不为当前用户收藏时 返回null*/
        SysUser user = SecurityUtils.getLoginUser().getUser();
        InvitationCollect invitationCollect = invitationCollectMapper.selectInvitationCollectById(id);
        if (!Objects.isNull(invitationCollect) && !Objects.isNull(invitationCollect.getUserId())) {

            if (user.getUserId() != invitationCollect.getUserId()) {
                return null;
            }
        } else {
            return null;
        }

        return invitationCollect;
    }

    /**
     * 查询用户收藏列表
     *
     * @param invitationCollect 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<InvitationCollect> selectInvitationCollectList(InvitationCollect invitationCollect) {

        /*
         * 如果当前用户身份不是管理员
         * 则查询自身的用药提醒
         * */
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        if (!Objects.isNull(roles)) {
            roles.forEach(bean -> {
                if (100 == bean.getRoleId().longValue()) {
                    invitationCollect.setUserId(user.getUserId());
                }
            });
        } else {
            invitationCollect.setUserId(user.getUserId());
        }

        List<InvitationCollect> invitationCollects = invitationCollectMapper.selectInvitationCollectList(invitationCollect);
        if (!Objects.isNull(invitationCollects)){
            invitationCollects.forEach(bean -> {

                /*获取当前帖子所属图片*/
                AttachFile attachFile = new AttachFile();
                attachFile.setSourceId(bean.getInvitationId());
                attachFile.setFileClass("03");
                List<AttachFile> attachFiles = AttachFileService.selectAttachFileList(attachFile);
                if (Objects.isNull(bean.getInvitation())){
                    bean.setInvitation(new Invitation());
                }
                    bean.getInvitation().setAttachFiles(attachFiles);

            });
        }

        return invitationCollects;
    }

    /**
     * 新增用户收藏
     *
     * @param invitationCollect 用户收藏
     * @return 结果
     */
    @Override
    public int insertInvitationCollect(InvitationCollect invitationCollect) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        /*如果状态是取消收藏*/
        if (!Objects.isNull(invitationCollect.getStatus()) && 1 == invitationCollect.getStatus()) {
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("invitationId",invitationCollect.getInvitationId());
            hashMap.put("userId",user.getUserId());

            return invitationCollectMapper.deleteInvitationCollectById(hashMap);





        } else {

            invitationCollect.setCreateTime(DateUtils.getNowDate());
            invitationCollect.setUserId(user.getUserId());
            invitationCollect.setCreateBy(user.getUserName());
            invitationCollect.setCreateName(user.getNickName());

            return invitationCollectMapper.insertInvitationCollect(invitationCollect);
        }
    }

    /**
     * 修改用户收藏
     *
     * @param invitationCollect 用户收藏
     * @return 结果
     */
    @Override
    public int updateInvitationCollect(InvitationCollect invitationCollect) {
        invitationCollect.setUpdateTime(DateUtils.getNowDate());
        return invitationCollectMapper.updateInvitationCollect(invitationCollect);
    }

    /**
     * 批量删除用户收藏
     *
     * @param ids 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteInvitationCollectByIds(Long[] ids) {
        return invitationCollectMapper.deleteInvitationCollectByIds(ids);
    }

    /**
     * 删除用户收藏信息
     *
     * @param invitationId 用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteInvitationCollectById(Long invitationId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("invitationId",invitationId);
        hashMap.put("userId",user.getUserId());

        return invitationCollectMapper.deleteInvitationCollectById(hashMap);
    }

    /*
     * 根据帖子ID查询帖子总收藏数
     * */
    public Long getInvitationCollectCount(Long invitationId) {
        return invitationCollectMapper.getInvitationCollectCount(invitationId);
    }
}
